package com.minivision.sdktestdemo.ui;

import android.content.res.Resources;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.bean.MusicBean;
import com.minivision.sdktestdemo.theme.utils.CustomThemeInterface;
import com.minivision.sdktestdemo.theme.utils.ThemeUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by devtang on 2016/5/25.
 */
public class MusicEditRecyclerAdapter extends RecyclerView.Adapter<MusicEditRecyclerAdapter.MusicEditHolder> {
    private List<MusicBean> mList;
    private Map<String, Boolean> mMap;
    private OnItemCheckedListener mListener;
    private int mCount;

    public MusicEditRecyclerAdapter(List<MusicBean> list) {
        mMap = new HashMap<>();
        mList = list;
        mCount = 0;
    }

    public interface OnItemCheckedListener {
        void onItemChecked(int position);
    }

    @Override
    public MusicEditHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_music_edit_item, parent, false);
        return new MusicEditHolder(view);
    }

    @Override
    public void onBindViewHolder(final MusicEditHolder holder, final int position) {
        holder.setItemCheckedListener(mListener, position);
        holder.mCheckBox.setChecked(isChecked(mList.get(position)));
        holder.mTvTitle.setText(mList.get(position).getmMusicName());
        holder.mTvContent.setText(mList.get(position).getmSingerName() + " - " + mList.get(position).getmMusicName());
        ThemeUtils.initTheme(holder.itemView);
    }

    public boolean isChecked(MusicBean bean) {
        return mMap.get(bean.getmPath()) == null ? false : mMap.get(bean.getmPath());
    }

    public void setChecked(MusicBean bean, boolean flag) {
        mMap.put(bean.getmPath(), flag);
    }

    public void setOnItemCheckedListener(OnItemCheckedListener listener) {
        mListener = listener;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void selectAllChecked() {
        for (MusicBean bean : mList) {
            mMap.put(bean.getmPath(), true);
        }
    }

    public void cancelAllChecked() {
        mMap.clear();
    }

    public List<MusicBean> removeAllChecked() {
        List<MusicBean> temp = new ArrayList<>();
        List<MusicBean> ans = new ArrayList<>();
        for (MusicBean bean : mList) {
            if (!isChecked(bean)) {
                temp.add(bean);
            } else {
                ans.add(bean);
            }
        }
        mList.clear();
        mList.addAll(temp);
        mMap.clear();
        mCount = 0;
        return ans;
    }

    public int getCheckedCount() {
        mCount = 0;
        for (MusicBean bean : mList) {
            mCount += isChecked(bean) ? 1 : 0;
        }
        return mCount;
    }

    public class MusicEditHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.checkbox)
        CheckBox mCheckBox;

        @Bind(R.id.tv_title)
        TextView mTvTitle;

        @Bind(R.id.tv_content)
        TextView mTvContent;

        @Bind(R.id.layout_item)
        LinearLayout mLayoutItem;

        public MusicEditHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void initTheme() {
            Resources.Theme theme = ThemeUtils.getTheme();
            if (theme != null) {
                ((CustomThemeInterface)mTvTitle).setTheme(theme);
                ((CustomThemeInterface)mTvContent).setTheme(theme);
                ((CustomThemeInterface)mLayoutItem).setTheme(theme);
            }
        }

        public void setItemCheckedListener(final OnItemCheckedListener listener, final int position) {
            if (listener != null) {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean flag = isChecked(mList.get(position));
                        mCheckBox.setChecked(!flag);
                    }
                });
                mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked(mList.get(position)) != isChecked) {
                            setChecked(mList.get(position), isChecked);
                            listener.onItemChecked(position);
                        }
                    }
                });
            }
        }
    }
}
